package com.yuwei.module_vehicles.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.lib_http.entity.VehiclesBody
import com.yuwei.module_vehicles.R
import com.yuwei.module_vehicles.adapter.CarProvincesAdapter
import com.yuwei.module_vehicles.databinding.ActivityAddCarBinding
import com.yuwei.module_vehicles.vm.VehiclesViewModel

/**
 *智慧园区-02-停车缴费V1.1-添加车位-20250507-崔志彪
 */
@Route(path = RouterPath.MODULE_ADD_CAR_ACTIVITY)
class AddCarActivity : BaseMvvmActivity<ActivityAddCarBinding>() {
    private val vm: VehiclesViewModel by viewModels()
    val carProvincesAdapter = CarProvincesAdapter()
    var mPosition = -1
    override fun initView() {
        binding.ivCarPeriod.setOnClickListener {
            ARouter.getInstance().build(RouterPath.MODULE_CAR_DISTINGUISH_ACTIVITY).navigation()
        }
        //省份简码
        binding.rvVehicleAddCars.adapter = carProvincesAdapter
        binding.rvVehicleAddCars.layoutManager = GridLayoutManager(this, 7)
        vm.getProvinces()
        carProvincesAdapter.setOnItemClickListener { adapter, view, position ->
            mPosition = position
            carProvincesAdapter.setPosition(mPosition)
            binding.btnCarNumber.text = carProvincesAdapter.data[mPosition].abbreviation
            carProvincesAdapter.notifyDataSetChanged()
        }
        binding.back.setOnClickListener {
            finish()
        }

        binding.tvAddCar.setOnClickListener {
            if (binding.etCarNumber.text.toString().isEmpty()) {
                ToastUtils.showShort("请输入车牌号")
                binding.etCarNumber.error = "请输入车牌号"
                return@setOnClickListener
            }
            val licensePlate = "${binding.btnCarNumber.text}${binding.etCarNumber.text}"
            val regex =
                Regex("^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][·]?[A-HJ-NP-Z0-9]{4,5}([A-HJ-NP-Z0-9挂学警港澳])?\$")
            if (!binding.etCarNumber.text.isEmpty()) {
                if (!regex.matches(licensePlate)) {
                    ToastUtils.showShort("车牌格式错误：请检查城市代码或新能源标识")
                    binding.etCarNumber.error = "车牌格式错误：请检查城市代码或新能源标识"
                    return@setOnClickListener
                }
            }

            val vehiclesBody = VehiclesBody(
                id = 0,
                licensePlate = licensePlate,
                ownerName = "张三",
                provinceAbbreviation = binding.btnCarNumber.text.toString(),
                userId = 1
            )
            vm.getAddCar(vehiclesBody)


        }
    }

    override fun loadData() {
        vm.getCarProvincesSuccess.observe(this) {
            carProvincesAdapter.data.addAll(it)
            carProvincesAdapter.notifyDataSetChanged()
        }
        vm.getAddCarSuccess.observe(this) {
            ToastUtils.showShort("添加成功")
            finish()
            ARouter.getInstance().build(RouterPath.MODULE_ADD_PAYMENTS_ACTIVITY)
                .withString("num", it.licensePlate)
                .withString("name", it.ownerName).navigation()
        }
    }

    override fun observeDataChange() {

    }

    override fun getLayoutId(): Int = R.layout.activity_add_car

}